<?php
if (empty($_GET['action'])) {
    $patternPhone = '/^[0-9\+]*$/';
    $patternEmail = '^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$';
    $smarty->assign('sent', isset($_GET['sent']));
    $smarty->assign('name', $_POST['name']);
    $smarty->assign('country_code', $_POST['country_code']);
    $smarty->assign('area_code', $_POST['area_code']);
    $smarty->assign('phone', $_POST['phone']);
    $phoneFit = eregi($patternPhone, $_POST['phone']);
    $smarty->assign('phone_fit', $phoneFit);
    $smarty->assign('email', $_POST['email']);
    $emailFit = eregi($patternEmail, $_POST['email']);
    $smarty->assign('email_fit', $emailFit);
    $smarty->assign('subject', $_POST['subject']);
    $smarty->assign('message_text', $_POST['message_text']);

    if (isset($_GET['sent']) && !empty($_POST['name']) && $_POST['email'] != 'Ihre@email.de'
        && !empty($_POST['subject']) && !empty($_POST['message_text']) && $emailFit
    ) {

        $host = $_SERVER['REMOTE_ADDR'];
        $phone = $_POST['country_code'] . $_POST['area_code'] . $_POST['phone'];
        $messageText = nl2br($_POST['message_text']);
        $messageText = str_replace('<br />', '\n', $messageText);

        mail($contact_email, $_POST['subject'],
             $messageText . '\n\n\n' . $_POST['name'] . '\n' . $_POST['email'] . '\n' . $phone . '\n\nIP: ' . $host,
             'From: ' . $_POST['email'] . '');
        $smarty->assign('inc_tpl', 'contact/sent.tpl');
    }

    $smarty->display('contact/main.tpl');
}
